package l.l.w.leetcode._056MergeIntervals;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by llw on 2017/11/19.
 */
public class _056MergeIntervals {


    public List<Interval> merge(List<Interval> intervals) {
        if(intervals == null || intervals.size() <=1) return intervals;
        Collections.sort(intervals,new Comparator<Interval>(){
            @Override
            public int compare(Interval o1, Interval o2) {
                return o1.start - o2.start;
            }
        });
        Interval pre = intervals.get(0);
        Interval cur;
        for(int i = 1; i < intervals.size();){
            cur = intervals.get(i);
            if(pre.end >= cur.start){
                pre.end = pre.end > cur.end?pre.end:cur.end;
                intervals.remove(i);
            }else{
                i++;
            }
        }
        return intervals;
    }
}
class Interval {
  int start;
  int end;
  Interval() { start = 0; end = 0; }
  Interval(int s, int e) { start = s; end = e; }
}
